import { postLoginAPI, postRegisterAPI, postSelectRole,postUpdatetAPI,postUserDeleteAPI} from "../../api/userinfo"

const user = {
  state: {
    token: "", //用户认证token
    userInfo: {}, //用户基本信息
    role:0,
    roleTag:''
  },

  mutations: {
    //保存用户基本信息
    SET_USERINFO(state: any, value: any) {
      state.userInfo = value.user;
      state.token = value.token;
      state.role=value.user.role;
    },
    SET_ROLE(state:any,value:any){
      state.roleTag=value.tag
    }
  },

  actions: {
    async SET_LOGIN(content: any, Login: any) {
      return new Promise(async resolve => {
        await postLoginAPI(Login).then((res) => {
          content.commit("SET_USERINFO", res.data);
          resolve(res.data)
        }).catch((err)=>{resolve(err)})
      })
    },
    //注册
    SET_REGISTER(content: any, Register: any) {
      return new Promise(async resolve => {
        await postRegisterAPI(Register).then((res) => {
          content.commit("SET_USERINFO", res.data)
          resolve(resolve)
        })
      })
    },
    //角色查询
    async SET_ROLE(content: any, value: any) {
      return new Promise(async resolve => {
      await postSelectRole(value).then((res) => {
        content.commit("SET_ROLE", res.data)
        resolve(resolve)
      })
    })
  },
  //信息修改
    async SET_UPDATEINFO(content: any, value: any) {
      return new Promise(async resolve => {
      await postUpdatetAPI(value).then(() => {
        resolve(resolve)
      })
    })
  },
  //删除用户
  async SET_DELETEINFO(content: any, value: any) {
    return new Promise(async resolve => {
    await postUserDeleteAPI(value).then(() => {
      resolve(resolve)
    })
  })
},
  }
}

export default user;
